package com.dotflat.physicalevaluate.controller;


import com.dotflat.physicalevaluate.domain.importexport.PhysicEvaluateImportRecord;
import com.dotflat.physicalevaluate.repository.ImportRecordRepository;
import com.dotflat.physicalevaluate.service.PhysicEvaluateService;
import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.page.PagerQuery;
import com.matt.domain.Classes;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.service.ClassesService;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.DELETE;
import static org.springframework.web.bind.annotation.RequestMethod.GET;

@RestController
@RequestMapping("/peapi/records")
@Api(value = "/peapi/records", tags = "PE - 导入记录")
public class PhysicalEvaluateRecordsController {

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private ClassesService classesService;

    @Autowired
    private PhysicEvaluateService physicEvaluateService;

    @Autowired
    private ImportRecordRepository importRecordRepository;


    @ApiOperation(value = "创建一个连续输入的导入记录")
    @RequestMapping(value = "/new_record", method = RequestMethod.POST)
    public StatusResponse newRecord(@RequestHeader(TICKET) String ticket, String classId) {
        StaffDTO staff = cache.getStaff(ticket);

        PhysicEvaluateImportRecord record = physicEvaluateService.newRecord(staff, classId);

        return StatusResponse.success(record);
    }

    @ApiOperation(value = "查询草稿导入记录")
    @RequestMapping(value = "/draft_record", method = RequestMethod.POST)
    public StatusResponse draftRecord(@RequestHeader(TICKET) String ticket, String classId) {
        StaffDTO staff = cache.getStaff(ticket);

        Classes clazz = classesService.getClassesById(staff.getAreaId(), classId);
        if (clazz == null){
            throw new BusinessException(ErrorCode.CLASSES_NOT_EXIST, "班级不存在");
        }

        PhysicEvaluateImportRecord record = physicEvaluateService.draftRecord(staff, classId);

        return StatusResponse.success(record);
    }


    @ApiOperation(value = "完成草稿导入记录，同时内部所有报告都会转成正式状态")
    @RequestMapping(value = "/finish_draft", method = RequestMethod.POST)
    public StatusResponse finishDraft(@RequestHeader(TICKET) String ticket, String recordId) {
        StaffDTO staff = cache.getStaff(ticket);

        PhysicEvaluateImportRecord record = physicEvaluateService.finishDraft(staff, recordId);

        return StatusResponse.success(record);
    }


    @ApiOperation(value = "下载某个导入记录对应的所有报告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "导入记录id，用以集合下载zip包", required = false),
            @ApiImplicitParam(name = "reportId", value = "单个下载某个报告， 跟上面二选一"),
            @ApiImplicitParam(name = "ticket", value = "票据", paramType = "header", required = false)

    })
    @RequestMapping(value = "/reports",  method = GET)
    public StatusResponse reports(@RequestHeader(TICKET) String ticket,
                                  String recordId, String reportId) {
        //TODO:
        return StatusResponse.success();
    }


    @ApiOperation(value = "查询某个记录列表",httpMethod = "GET")
    @RequestMapping("/records")
    public StatusResponse evaluatePage(@RequestHeader(TICKET) String ticket,
                                       String classId,
                                       PagerQuery query) {
        String staffId = cache.getStaffId(ticket);

        PageRequest pageRequest = PageRequest.of(query.getPageNum(), query.getPageSize());
        Page<PhysicEvaluateImportRecord> page = importRecordRepository.findAllByCreateTeacherIdAndClassIdAndFinishedIsTrueOrderByCreatedDesc(staffId, classId,pageRequest );

//        Page<EvaluateImportRecordVO> ret = page.map(record -> EvaluateImportRecordVO.of(record));

        return StatusResponse.success(page);
    }

    @ApiOperation(value = "删除某个导入记录",httpMethod = "DELETE")
    @RequestMapping(value = "/record", method = DELETE)
    public StatusResponse deleteRecord(@RequestHeader(TICKET) String ticket, String evaluateRecordId) {
        StaffDTO staff = cache.getStaff(ticket);
        boolean ret = physicEvaluateService.deleteReport(staff, evaluateRecordId);
        return StatusResponse.success(ret);
    }

    @ApiOperation(value = "导入记录详情",httpMethod = "GET")
    @RequestMapping(value = "/record", method = GET)
    public StatusResponse recordDetail(@RequestHeader(TICKET) String ticket,
                                       String evaluateRecordId) {
        String staffId = cache.getStaffId(ticket);

        return StatusResponse.success(physicEvaluateService.getByStaffIdAndId(staffId, evaluateRecordId));
    }

}
